<?php

/********************************************
时间：2018.10.24
作者：王素臣
用途：研磨车间称重接口
备注：
 ********************************************/

namespace App\Http\Controllers\api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use App\Http\Model\Admin\SysPublic;

class ArtdetweightController extends Controller
{
    /**
     * @return string
     */
    /********************************************
    时间：2018.10.24
    作者：王素臣
    用途：锻造车间研磨工序称重添加记录接口函数——修改和添加 升级为 锻造车间工序称重，实现称重数据的添加和修改
    备注：指定执行计划号、箱号、工序记录不存在，insert记录
     *    若记录存在，检查已存在记录的称重人和提交的称重人是否一致，不一致报301错误，一致，在检查该计划执行号、箱号的后续
     *   工序是否存在，不存在，改写记录，存在，报304错误。
     * {"errorcode":"301","writemode":"","bill_no":"","success":"false"}称重人不一致
     * {"errorcode":"302","writemode":"update","bill_no":"","success":"false"}修改称重记录出现异常
     * {"errorcode":"303","writemode":"update","bill_no":"'.$rd_bill_no.'","success":"false"} 改写不成功，原因1.数据未发生变化
     * {"errorcode":"304","writemode":"","bill_no":"","success":"false"} 后续工序已经称重，不允许修改
     * {"errorcode":"200","writemode":"update","bill_no":"'.$rd_bill_no.'","success":"true"} 改写成功
     *
     * {"errorcode":"401","writemode":"inset","bill_no":"","success":"false"} 插入数据异常
     * {"errorcode":"402","writemode":"inset","bill_no":"","success":"false"} 插入数据发生错误
     * {"errorcode":"200","writemode":"inset","bill_no":"'.$bill_no.'","success":"true"}插入数据成功
    修改：2018.11.09
     *   2018.11.29
     ********************************************/
    public function insert() {
//        $message = '{"errorcode":"500","writemode":"","bill_no":"","success":"false"}';
        if ($input = Input::all()) {
            $data['exec_qrcode'] = $input['exec_qrcode'];    // '执行计划号',
            $data['print_code']  = $input['print_code'];     //'箱号',
            $data['artdet_id']   = $input['artdet_id'];     //'当前工序 序号',
            $data['work_order']  = $input['work_order'];     //'当前工序',
            $data['box_tare']    = $input['box_tare'];        //'箱皮重',
            $data['box_gross']   = $input['box_gross'];      //'箱毛重',
            $data['box_net']     = $input['box_net'];         //'箱净重',
            $data['unit_sample_weight'] = $input['unit_sample_weight'];     //'单个工件采样重量',
            $data['box_workpiece_number'] = $input['box_workpiece_number']; //'箱子工件数量',
            $data['o_valiable_num']       = $input['o_valiable_num'];       //合格数量
            //$data['usestate']   = $input['usestate'];                         //'使用标志 Web端',
            $data['modman'] = $input['modman'];                           //'制单人',
            $data['moddate'] = $input['moddate'];
            $selectrd=DB::select('select * from t_artdet_weight_detail where exec_qrcode=? and print_code=? and work_order=?',
                                 [$data['exec_qrcode'], $data['print_code'], $data['work_order'] ]
                                );
            if ($selectrd){    //指定执行计划号，箱号，工序的记录存在
                foreach ($selectrd as $rd){
                    $rdid = $rd->ID;
                    $rd_bill_no = $rd->bill_no;
                    if ($data['modman']==$rd->modman){    //重新称重人与数据记录的称重人一致
                        //检索是否是当前已称重工序的最终工序
                        $selectsubrd=DB::select('select max(work_order) as max_work_order from t_artdet_weight_detail where '
                            .'exec_qrcode=? and print_code=?',[$data['exec_qrcode'] ,$data['print_code']]);
                        if ($selectsubrd<>null){
                            foreach ($selectsubrd as $subrd){
                                if ($subrd->max_work_order ==$data['work_order']){    //当前已经称重工序的最大工序等于该次称重工序
                                    try{
                                        DB::beginTransaction();
                                        $update=DB::update('update t_artdet_weight_detail set box_tare='.$data['box_tare'].
                                            ',box_gross='.$data['box_gross'].',box_net='.$data['box_net'].
                                            ',unit_sample_weight='.$data['unit_sample_weight'].',box_workpiece_number='.$data['box_workpiece_number'].
                                            ',o_valiable_num='.$data['o_valiable_num'].',moddate='.$data['moddate'].//',modman="'.$data['modman'].'"'.
                                        ' where id='.$rdid);
                                        if ($update){    //改写成功
                                            DB::commit();
                                            $message = '{"errorcode":"200","writemode":"update","bill_no":"'.$rd_bill_no.'","success":"true"}';
                                        }else{           //改写不成功
                                            DB::rollBack();
                                            $message = '{"errorcode":"303","writemode":"update","bill_no":"'.$rd_bill_no.'","success":"false"}';
                                        }
                                    }
                                    catch (\Exception $e) {
                                        DB::rollBack();
                                        $message = '{"errorcode":"302","writemode":"update","bill_no":"","success":"false"}';
                                    }
                                }else{                                                 //当前已经称重工序的最大工序不等于该次称重工序
                                    $message = '{"errorcode":"304","writemode":"","bill_no":"","success":"false"}';
                                }
                                break;
                            }
                        }
                    } else{                                //重新称重人与数据记录的称重人不一致
                        $message = '{"errorcode":"301","writemode":"","bill_no":"","success":"false"}';
                    }
                    break;
                }
            }
            else{             //指定执行计划号，箱号，工序的记录不存在
                //insert记录
                try{
                    DB::beginTransaction();
                    $bill_no = SysPublic::getautocode('GrindWeight');
                    $insertrd=DB::insert('insert into t_artdet_weight_detail (bill_no,exec_qrcode,print_code,artdet_id
                        ,work_order,box_tare,box_gross,box_net ,unit_sample_weight,box_workpiece_number,o_valiable_num
                        ,modman,moddate) values(?,?,?,?,?,?,?,?,?,?,?,?,?)',
                        [$bill_no,$data['exec_qrcode'],$data['print_code'],$data['artdet_id'],$data['work_order']
                            ,$data['box_tare'],$data['box_gross'],$data['box_net'],$data['unit_sample_weight']
                            ,$data['box_workpiece_number'],$data['o_valiable_num'],$data['modman'],$data['moddate']
                        ]
                    );
                    if ($insertrd){
                        DB::commit();
                        $message = '{"errorcode":"200","writemode":"inset","bill_no":"'.$bill_no.'","success":"true"}';
                    }else{
                        DB::rollBack();
                        $message = '{"errorcode":"402","writemode":"inset","bill_no":"","success":"false"}';
                    }
                }
                catch(\Exception $e){
                    DB::rollBack();
//                    $message = '{"errorcode":"401","writemode":"inset","bill_no":"","success":"false"}';
                    $message = '{"errorcode":"401","writemode":"inset","bill_no":"","success":"false"}';
                }
            }
        }
        return $message;
    }

    /********************************************
    时间：2018.10.24
    作者：王素臣
    用途：车间工序称重记录查询接口
    备注：返回近一个月称重数据
    修改：2018.11.09
     ********************************************/
    public function select() {
        $start_time = strtotime(date("Y-m-d"));
        $end_time = strtotime("-1 month", $start_time);
        $selectRd = DB::select('select * from t_artdet_weight_detail where moddate>=? ', [$end_time]);
        return response()->json($selectRd);  ;
    }

}
